<?php

class Empeval_Notifications_FillForm {
	
	private $config;
	private $enabled;
	
	public function __construct() {
		$this->config = Empeval_Config::getInstance()->notifications->fillForm;
		$this->enabled = (bool)$this->config->enabled;
	}
	
	public function addFirstNotifications() {
		if (!$this->enabled)
			return;
		$date = new DateTime('now');
		$date->modify('+12 day');
		$users = EmpevalViewFillFormNotificationQuery::create()->filterByFillDeadline($date)->find();
		if ($users->count() > 0) {
			$tpl = EmpevalNotificationTemplatesQuery::create()->findOneByName(EmpevalNotificationTemplates::FIRST_FILL_NOTIFICATION);
			if (!($tpl instanceof EmpevalNotificationTemplates))
				throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_TPL_NOT_EXIST);
			$contentText = $this->addContentData($tpl->getContentText(), $date);
			$contentHtml = $this->addContentData($tpl->getContentHtml(), $date);
			
			$con = Propel::getConnection('vgp_empeval');
			$con->beginTransaction();
			try {
				$notification = new EmpevalNotifications();
				$notification->setStatus(EmpevalNotifications::STATUS_TO_SEND);
				$notification->setNotificationTemplates($tpl);
				$notification->save($con);
				$notificationData = new EmpevalNotificationData();
				$notificationData->setSubject($tpl->getSubject());
				$notificationData->setContentText($contentText);
				$notificationData->setContentHtml($contentHtml);
				$notificationData->setNotifications($notification);
				$notificationData->save($con);
				foreach ($users as $user) {
					$notificationUser =new  EmpevalNotificationUsers();
					$notificationUser->setUserId($user->getUserId());
					$notificationUser->setNotifications($notification);
					$notificationUser->setEmail($user->getEmail());
					$notificationUser->save($con);
				}
				$con->commit();
			} catch (Exception $e) {
				$con->rollBack();
				throw $e;
			}
		}
	}
	
	public function addSecondNotifications() {
		if (!$this->enabled)
			return;
		$date = new DateTime('now');
		$users = EmpevalViewFillFormNotificationQuery::create()
		->usePositionsQuery(null, Criteria::LEFT_JOIN)
			->useUserPositionsQuery(null, Criteria::LEFT_JOIN)
				->leftJoinUsers()
			->endUse()
		->endUse()
		->addJoinCondition('UserPositions', 'UserPositions.Active = 1')
		->filterByFillDeadline($date)
		->find();
		if ($users->count() > 0) {
			$tpl = EmpevalNotificationTemplatesQuery::create()->findOneByName(EmpevalNotificationTemplates::SECOND_FILL_NOTIFICATION);
			if (!($tpl instanceof EmpevalNotificationTemplates))
				throw new Empeval_Exception_App(Empeval_Exception_Codes::ERROR_TPL_NOT_EXIST);
			$contentText = $this->addContentData($tpl->getContentText());
			$contentHtml = $this->addContentData($tpl->getContentHtml());
			$con = Propel::getConnection('vgp_empeval');
			$con->beginTransaction();
			try {
				$notification = new EmpevalNotifications();
				$notification->setStatus(EmpevalNotifications::STATUS_TO_SEND);
				$notification->setNotificationTemplates($tpl);
				$notification->save($con);
				$notificationData = new EmpevalNotificationData();
				$notificationData->setSubject($tpl->getSubject());
				$notificationData->setContentText($contentText);
				$notificationData->setContentHtml($contentHtml);
				$notificationData->setNotifications($notification);
				$notificationData->save($con);
				foreach ($users as $user) {
					$notificationUser =new  EmpevalNotificationUsers();
					$notificationUser->setUserId($user->getUserId());
					$notificationUser->setNotifications($notification);
					$notificationUser->setEmail($user->getEmail());
					$notificationUser->save($con);
					if ($user->getPositions() !== null && $user->getPositions()->getUserPositionss()->getFirst() !== null) {
						$notificationUser = new  EmpevalNotificationUsers();
						$notificationUser->setUsers($user->getPositions()->getUserPositionss()->getFirst()->getUsers());
						$notificationUser->setNotifications($notification);
						$notificationUser->setEmail($user->getEmail());
						$notificationUser->save($con);
					}
				}
				$con->commit();
			} catch (Exception $e) {
				$con->rollBack();
				throw $e;
			}
		}
	}
	
	private function addContentData($content, $date=null) {
		if ($date instanceof DateTime)
			$content = str_replace('{$date}', $date->format('d.m.Y'), $content);
		$content = str_replace('{$link}', Empeval_Config::getInstance()->baseUrl . $this->config->data->myFormListUrl, $content);
		return $content;
	}
	
}